package xyz.hujf.tool.CyclicBarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Hujf
 * @title: Test1
 * @date 2021-05-03 15:59
 * @description: TODO
 */
public class Test1 {

    private static CyclicBarrier cyclicBarrier = new CyclicBarrier(3,()->{
        System.out.println(Thread.currentThread()+" task merge result");
    });

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        executorService.submit(()->{
            System.out.println(Thread.currentThread()+" task1-1");
            System.out.println(Thread.currentThread()+"enter in ba");
            try {
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread()+" out");
        });

        executorService.submit(()->{
            System.out.println(Thread.currentThread()+" task2-2");
            System.out.println(Thread.currentThread()+"enter in ba");
            try {
                Thread.sleep(10000);
                cyclicBarrier.await();

            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }

            System.out.println(Thread.currentThread()+" out");
        });

        executorService.submit(()->{
            System.out.println(Thread.currentThread()+" task3-3");
            System.out.println(Thread.currentThread()+"enter in ba");
            try {
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread()+" out");
        });

        executorService.shutdown();
    }
}
